import { Router } from 'express';
import { MessageController } from '@/controllers/message';
import { authenticateToken as auth } from '@/middleware/auth';
import validateRequest from '@/middleware/validateRequest';
import { body, param, query } from 'express-validator';

const router = Router();
const messageController = new MessageController();

// 获取聊天消息
router.get('/',
  auth,
  [
    query('page').optional().isInt({ min: 1 }).withMessage('页码必须是正整数'),
    query('limit').optional().isInt({ min: 1, max: 100 }).withMessage('每页数量必须在1-100之间'),
  ],
  validateRequest,
  messageController.getMessages
);

// 发送消息
router.post('/',
  auth,
  [
    body('content').notEmpty().withMessage('消息内容不能为空'),
    body('type').optional().isIn(['text', 'image', 'file']).withMessage('消息类型无效'),
  ],
  validateRequest,
  messageController.sendMessage
);

// 标记指定消息为已读
router.put('/:messageId/read',
  auth,
  [
    param('messageId').isUUID().withMessage('消息ID格式无效'),
  ],
  validateRequest,
  messageController.markAsRead
);

// 标记所有消息为已读
router.put('/read-all', auth, messageController.markAllAsRead);

// 获取未读消息数量
router.get('/unread-count', auth, messageController.getUnreadCount);

export { router as messageRoutes };
export default router;